package com.lc.hongtai.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.hongtai.MyApplication;
import com.lc.hongtai.R;
import com.lc.hongtai.conn.JsonAccessToken;
import com.lc.hongtai.conn.JsonInvestdetail;
import com.lc.hongtai.toast.LovelyToast;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;


public class InvestmentActivity extends BaseActivity implements View.OnClickListener {


    private ImageView ivback;
    private TextView tvname;
    private TextView tvsumMoney;
    private TextView tvrate;
    private TextView tvtime;


    private TextView tvinvest05;


    private Button btinvestnow;
    private String borrow_status, id;


    private String str;

    public static String getProduct;
    @BoundView(R.id.layout_product_detail)
    private LinearLayout layout_product_detail;

    @BoundView(R.id.img_selected)
    private ImageView img_selected;

//投资金额

    @BoundView(R.id.editText_money)
    private EditText editText_money;
    private int num = 1000;
    //增加钱数
    @BoundView(R.id.img_addmoney)
    private ImageView img_addmoney;

    //减少钱数
    @BoundView(R.id.img_reduce)
    private ImageView img_reduce;
    @BoundView(R.id.text_invest_know)
    private TextView text_invest_know;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_investment);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        initView();
        editText_money.setText(String.valueOf(num));

        getAccessToken();

        btinvestnow.setOnClickListener(this);
        text_invest_know.setOnClickListener(this);

    }


    private void getAccessToken() {
        new JsonAccessToken("index/invest_detail", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                if (info != null) {
                    if (!TextUtils.isEmpty(info.access_token)) {
                        initData(info.access_token);
                    } else {
                        // UtilToast.show(InvestmentActivity.this, "安全验证失败！");
                        LovelyToast.makeText(InvestmentActivity.this, "安全验证失败", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                // UtilToast.show(InvestmentActivity.this, "安全验证失败！");
                LovelyToast.makeText(InvestmentActivity.this, "安全验证失败", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }
        }).execute(this, true);

    }


    private void initData(String access_token) {
        new JsonInvestdetail(access_token, getIntent().getStringExtra("id"), "borrowinfo", new AsyCallBack<JsonInvestdetail.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonInvestdetail.Info info) throws Exception {
                if (info != null) {
                    Log.d(getIntent().getStringExtra("id"),"333333333333333");
                    Log.d(getIntent().getStringExtra("id"),"333333333333333");
                    tvname.setText(info.borrowinfo.borrow_name);
                    tvsumMoney.setText(info.borrowinfo.has_borrow);
                    tvrate.setText(info.borrowinfo.borrow_interest_rate + "%");
                    tvtime.setText(info.borrowinfo.borrow_duration + info.borrowinfo.borrow_duration_cn);
                    //tvtimeUnit.setText(info.borrowinfo.borrow_duration_cn);

                    tvinvest05.setText(info.borrowinfo.invest_num);


                    borrow_status = info.borrowinfo.borrow_status;
                    id = info.borrowinfo.id;


                    getProduct = info.borrowinfo.borrow_name;

                    tvname.setText(info.borrowinfo.borrow_name);

                } else {
                    // UtilToast.show(InvestmentActivity.this, JsonInvestdetail.TOAST);
                    LovelyToast.makeText(InvestmentActivity.this, JsonInvestdetail.TOAST, 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {

            }

        }).execute(this, false);
    }

    private void initView() {
        this.btinvestnow = (Button) findViewById(R.id.bt_invest_now);

        this.tvinvest05 = (TextView) findViewById(R.id.tv_invest_05);


        this.tvtime = (TextView) findViewById(R.id.tv_time);
        this.tvrate = (TextView) findViewById(R.id.tv_rate);
        this.tvsumMoney = (TextView) findViewById(R.id.tv_sumMoney);
        this.tvname = (TextView) findViewById(R.id.tv_name);
        this.ivback = (ImageView) findViewById(R.id.iv_back);
        // this.tvtitle = (TextView) findViewById(R.id.tv_title);
        layout_product_detail.setOnClickListener(this);
//
        img_selected.setOnClickListener(this);
        img_addmoney.setOnClickListener(this);
        img_reduce.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.bt_invest_now:
                if (TextUtils.isEmpty(MyApplication.myPreferences.readUid())) {
                    //  ToastUtils.show(this, "请登录后再操作！");
                    LovelyToast.makeText(InvestmentActivity.this, "请登录后再操作！", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!TextUtils.isEmpty(borrow_status) && borrow_status.equals("2")) {
                    Intent intent = new Intent();
                    intent.putExtra("id", id);
                    intent.putExtra("mode", true);
                    intent.putExtra("product", getProduct);
                    intent.setClass(this, PayActivity.class);
                    startActivity(intent);
                } else {

                   /* LovelyToast.makeText(InvestmentActivity.this, "该项目不能投资！", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);*/
                    Intent intent = new Intent();
                    intent.putExtra("id", id);
                    intent.putExtra("mode", true);
                    intent.putExtra("product", getProduct);
                    intent.setClass(this, PayActivity.class);
                    startActivity(intent);
                }
                break;


            case R.id.layout_product_detail:
                // startActivity(new Intent(InvestmentActivity.this, ProductDetailActivity.class));
                Intent intent = new Intent();

                intent.putExtra("url", "http://vhost906.zihaistar.com/api/index/invest_detail?id=" + id + "&show_type=borrow_info");
                intent.putExtra("url1", "http://vhost906.zihaistar.com/api/index/invest_detail?id=" + id + "&show_type=invest_list");
                intent.setClass(InvestmentActivity.this, ProductDetailActivity.class);
                startActivity(intent);

                break;

            case R.id.img_selected:
                if (!img_selected.isSelected()) {
                    img_selected.setSelected(true);
                } else {
                    img_selected.setSelected(false);
                }


                break;

            //增加钱数
            case R.id.img_addmoney:
                this.num++;
                editText_money.setText(String.valueOf(this.num));
                break;
            case R.id.img_reduce:
                this.num--;
                editText_money.setText(String.valueOf(this.num));
                break;
          /*

           img_addmoney.setOnClickListener(this);
            img_reduce.setOnClickListener(this);*/

            case R.id.text_invest_know:
                startActivity(new Intent(InvestmentActivity.this, InvestKonwActivity.class));

                break;


        }
    }


}
